package com.zq.recruit.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zq.recruit.dao.dto.JobPositionDto;
import com.zq.recruit.dao.entity.Company;
import com.zq.recruit.dao.entity.JobPosition;
import com.zq.recruit.mapper.CompanyMapper;
import com.zq.recruit.mapper.JobPositionMapper;
import com.zq.recruit.service.JobPositionService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 张乔
* @description 针对表【job_position】的数据库操作Service实现
* @createDate 2025-03-16 16:38:35
*/
@Service
public class JobPositionServiceImpl extends ServiceImpl<JobPositionMapper, JobPosition>
    implements JobPositionService{


    @Resource
    private CompanyMapper companyMapper;



    @Override
    public PageInfo<JobPosition> findByPage(JobPositionDto sysUserDto, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<JobPosition> queryWrapper=new LambdaQueryWrapper<>();
//        创建查询条件，用户名模糊查询，创建时间范围查询
        queryWrapper.like(sysUserDto.getPositionName()!=null&&!StrUtil.isEmpty(sysUserDto.getPositionName())
                        ,JobPosition::getPositionName,
                        sysUserDto.getPositionName())
                .ge(sysUserDto.getCreateTimeBegin()!=null&&!StrUtil.isEmpty(sysUserDto.getCreateTimeBegin())
                        ,JobPosition::getCreatedAt,
                        sysUserDto.getCreateTimeBegin())
                .le(sysUserDto.getCreateTimeEnd()!=null&&!StrUtil.isEmpty(sysUserDto.getCreateTimeEnd())
                        ,JobPosition::getCreatedAt,
                        sysUserDto.getCreateTimeEnd())
                .orderByDesc(JobPosition::getId);
        PageHelper.startPage(pageNum, pageSize);
        List<JobPosition> userList = this.list(queryWrapper);
        for(JobPosition jobPosition:userList){
            if(jobPosition.getCompanyId()!=null){
                Company company = companyMapper.selectById(jobPosition.getCompanyId());
                jobPosition.setCompanyName(company.getCompanyName());
            }
        }
        PageInfo<JobPosition> page = new PageInfo<>(userList);
        return page;
    }

    @Override
    public List<JobPosition> listAll() {
        List<JobPosition> list = this.list();
        for(JobPosition jobPosition:list){
            if(jobPosition.getCompanyId()!=null){
                Company company = companyMapper.selectById
                        (jobPosition.getCompanyId());
                jobPosition.setCompanyName(company.getCompanyName());
            }
        }

        return list;
    }

    @Override
    public List<JobPosition> getJobByName(JobPositionDto sysUserDto) {
        LambdaQueryWrapper<JobPosition> queryWrapper=new LambdaQueryWrapper<>();
//        创建查询条件，用户名模糊查询，创建时间范围查询
        queryWrapper.like(sysUserDto.getPositionName()!=null&&!StrUtil.isEmpty(sysUserDto.getPositionName())
                ,JobPosition::getPositionName,
                sysUserDto.getPositionName());
        List<JobPosition> userList = this.list(queryWrapper);
        for(JobPosition jobPosition:userList){
            if(jobPosition.getCompanyId()!=null){
                Company company = companyMapper.selectById
                        (jobPosition.getCompanyId());
                jobPosition.setCompanyName(company.getCompanyName());
            }
        }

        return userList;
    }
}




